package com.redsoil.house.ui;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.redsoil.house.R;
import com.redsoil.house.app.AppContext;
import com.redsoil.house.ui.login.activity.LoginActivity;
import com.redsoil.house.ui.main.activity.MainActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.weidingqiang.rxfiflibrary2.base.SimpleActivity;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;
import io.reactivex.functions.Consumer;

/**
 * 欢迎页面
 * 调整用户模块
 * 接入组件库    腾讯组件库 （https://github.com/Tencent/QMUI_Android）
 *
 * 1.百度： 用户名16600268043   密码：qaz123456
 *

 问题：
 2.确认事项
 3.图片放大

 *
 * 数值需要 * 1.44
 */
public class WelcomeActivity extends SimpleActivity {

    private LinearLayout welcome_bg;

    private RelativeLayout banner_guide;

    private BGABanner mBackgroundBanner;
//    private BGABanner mForegroundBanner;

    private int recordPermissions = 0;

    @Override
    protected int getLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initEventAndData() {

        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }

        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        welcome_bg = (LinearLayout) this.findViewById(R.id.welcome_bg);
        banner_guide = (RelativeLayout) this.findViewById(R.id.banner_guide);

        //判断第一次进入
        SharedPreferences shared=getSharedPreferences("is", MODE_PRIVATE);
        boolean isfer=shared.getBoolean("isfer", true);
        SharedPreferences.Editor editor=shared.edit();
        if(isfer){
            //第一次

            editor.putBoolean("isfer", false);
            editor.commit();

            banner_guide.setVisibility(View.VISIBLE);
            welcome_bg.setVisibility(View.GONE);

            mBackgroundBanner = (BGABanner)this.findViewById(R.id.banner_guide_background);
//            mForegroundBanner = (BGABanner) this.findViewById(R.id.banner_guide_foreground);

            setListener();
            processLogic();
        }
        else {
            //第二次进入跳转
            welcome_bg.setVisibility(View.VISIBLE);

            Animation animation = AnimationUtils.loadAnimation(this, R.anim.app_start_anim);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }
                //动画完成
                @Override
                public void onAnimationEnd(Animation animation) {
                    initApp(animation);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            welcome_bg.setAnimation(animation);
        }

    }

    private void setListener() {
        /**
         * 设置进入按钮和跳过按钮控件资源 id 及其点击事件
         * 如果进入按钮和跳过按钮有一个不存在的话就传 0
         * 在 BGABanner 里已经帮开发者处理了防止重复点击事件
         * 在 BGABanner 里已经帮开发者处理了「跳过按钮」和「进入按钮」的显示与隐藏
         */
        mBackgroundBanner.setEnterSkipViewIdAndDelegate(R.id.btn_guide_enter, R.id.tv_guide_skip, new BGABanner.GuideDelegate() {
            @Override
            public void onClickEnterOrSkip() {
//                startActivity(new Intent(GuideActivity.this, MainActivity.class));
                initApp(null);
            }
        });
    }

    private void processLogic() {
        // Bitmap 的宽高在 maxWidth maxHeight 和 minWidth minHeight 之间
        BGALocalImageSize localImageSize = new BGALocalImageSize(720, 1280, 320, 640);
        // 设置数据源
        mBackgroundBanner.setData(localImageSize, ImageView.ScaleType.CENTER_CROP,
                R.drawable.uoko_guide_background_1,
                R.drawable.uoko_guide_background_2,
                R.drawable.uoko_guide_background_3,
                R.drawable.uoko_guide_background_4);

//        mForegroundBanner.setData(localImageSize, ImageView.ScaleType.CENTER_CROP,
//                R.drawable.uoko_guide_foreground_1,
//                R.drawable.uoko_guide_foreground_2,
//                R.drawable.uoko_guide_foreground_3);
    }

    private void initApp(Animation animation){

        if(!AppContext.getInstance().initLogin())
        {
            if(animation != null){
                welcome_bg.setAnimation(animation);
            }
            return;
        }

        RxPermissions rxPermissions=new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean){

                    if(AppContext.getInstance().isLogin())
                    {
                        startActivity(MainActivity.newInstance(getApplicationContext()));
                    }
                    else{
                        startActivity(LoginActivity.newInstance(getApplicationContext()));
                    }

                    finish();
                }else{
                    //只要有一个权限被拒绝，就会执行
                    ToastUtils.showLongToast("请开启相关权限");
                    finish();
                }
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();

        // 如果开发者的引导页主题是透明的，需要在界面可见时给背景 Banner 设置一个白色背景，避免滑动过程中两个 Banner 都设置透明度后能看到 Launcher
        if(mBackgroundBanner != null){
            mBackgroundBanner.setBackgroundResource(android.R.color.white);
        }
    }
}
